
package com.wyzz.hr.base.wf.overtimerequest;

import java.math.BigDecimal;
import java.util.*;
import org.apache.commons.lang.StringUtils;

import com.wyzz.hr.base.ComonBeans;
import com.wyzz.hr.base.Status;
import com.wyzz.hr.base.wf.AbstractWfApproverAction;
import com.wyzz.hr.bo.IAttdoriginaldataBO;
import com.wyzz.hr.bo.IAttendshiftBO;
import com.wyzz.hr.bo.IEmployeeBo;
import com.wyzz.hr.bo.IEmpshiftBo;
import com.wyzz.hr.bo.ILeavetypeBO;
import com.wyzz.hr.bo.IOvertimerequestBo;
import com.wyzz.hr.bo.IOvertimetypeBo;
import com.wyzz.hr.bo.ISysLogBO;
import com.wyzz.hr.bo.impl.ExaminDateUtil;
import com.wyzz.hr.pojo.Attdoriginaldata;
import com.wyzz.hr.pojo.Attendshift;
import com.wyzz.hr.pojo.Employee;
import com.wyzz.hr.pojo.Leavetype;
import com.wyzz.hr.pojo.Overtimerequest;
import com.wyzz.hr.pojo.Overtimetype;
import com.wyzz.hr.spring.SpringBeanFactory;
import com.wyzz.hr.util.DatabaseSysConfigManager;
import com.wyzz.hr.util.DateUtil;
import com.wyzz.hr.util.StringUtil;
import com.wyzz.hr.util.SystemPropertiesReader;
import com.wyzz.hr.vo.ExaminSearchBean;
import com.wyzz.hr.vo.OvertimeFormBean;

public class OrWfApproverAction extends AbstractWfApproverAction {

	public OrWfApproverAction() {
	}

	public void initOTLists() {
		deptList = getDrillDown("Department", new String[] { "1" });
		otTypeList = getDrillDown("OvertimeType", new String[0]);
		statusMap = ComonBeans.getValuesToMap(Overtimerequest.PROP_OR_STATUS,
				Status.PROCESS_MAP, new boolean[] { false });
	}

	public String beforeInit(String operate, Object formBean) {
		String msgRejComment = "\u62D2\u7EDD\u7533\u8BF7\u65F6\uFF0C\u5FC5\u987B\u586B\u5199\u5907\u6CE8\uFF01";
		String msgModComment = "\u4FEE\u6539\u540E\u5BA1\u6279\uFF0C\u5FC5\u987B\u586B\u5199\u5907\u6CE8\uFF01";
		String msgCanComment = "\u7533\u8BF7\u4F5C\u5E9F\u65F6\uFF0C\u5FC5\u987B\u586B\u5199\u5907\u6CE8\uFF01";
		String msgBeginTooBig = "\u52A0\u73ED\u5F00\u59CB\u65F6\u95F4({0})\u5FC5\u987B\u65E9\u4E8E\u7ED3\u675F\u65F6\u95F4({1})\uFF01";
		String msgEndTooBig = "\u9694\u591C\u52A0\u73ED\u7ED3\u675F\u65F6\u95F4\u4E0D\u80FD\u4E3A24\u70B9\uFF01";
		String msgOwnSubmit = "\u7CFB\u7EDF\u4E0D\u5141\u8BB8\u4ED6\u4EBA\u4EE3\u586B\u52A0\u73ED\u7533\u8BF7\uFF01";
		String msgEarlyDays = "\u5FC5\u987B\u63D0\u524D{0}\u5929\u63D0\u51FA\u52A0\u73ED\u7533\u8BF7\uFF01";
		String msgTiaoxiu = "\u8BF7\u8BBE\u7F6E\u8C03\u4F11\u5C0F\u65F6\u6570\uFF01";
		String msgOver24Hours = "\u52A0\u73ED\u65F6\u95F4\u8D85\u8FC724\u5C0F\u65F6\uFF01";
		OvertimeFormBean of_Bean = (OvertimeFormBean) formBean;
		if (StringUtils.isEmpty(of_Bean.getOrAppComment())) {
			if (operate.contains("reject"))
				return msgRejComment;
			if (operate.contains("modify"))
				return msgModComment;
			if (operate.contains("cancel"))
				return msgCanComment;
		}
		Date startDate = DateUtil.parseDateTimeToD(of_Bean.getOrShiftDate(),
				of_Bean.getStartTime().intValue(), of_Bean.getStartTimeMinute()
						.intValue(), 0);
		Date endDate = DateUtil.parseDateTimeToD(of_Bean.getOrShiftDate(),
				of_Bean.getEndTime().intValue(), of_Bean.getEndTimeMinute()
						.intValue(), 0);
		if (of_Bean.getStartOvernight().intValue() == 1)
			startDate = DateUtil.dateAdd(startDate, 1);
		if (of_Bean.getEndOvernight().intValue() == 1)
			endDate = DateUtil.dateAdd(endDate, 1);
		of_Bean.setStartDate(startDate);
		of_Bean.setEndDate(endDate);
		if (startDate.compareTo(endDate) >= 0)
			return StringUtil.message(msgBeginTooBig, new Object[] { startDate,
					endDate });
		if (DateUtil.dateDiff(startDate, endDate, 10) > 24)
			return StringUtil.message(msgOver24Hours, new Object[0]);
		if (of_Bean.getEndOvernight().intValue() == 1
				&& of_Bean.getEndTime().intValue() == 24)
			return StringUtil.message(msgEndTooBig, new Object[0]);
		if (!of_Bean.getOrEmpNo().equals(getCurrentEmp())) {
			if ((operate.contains("create") || operate.contains("update"))
					&& "0".equals(DatabaseSysConfigManager.getInstance()
							.getProperty("sys.examin.create.level")))
				return msgOwnSubmit;
		} else if (operate.contains("create")) {
			SystemPropertiesReader properties = SystemPropertiesReader
					.getInstance();
			String remind = properties.getValue("OR.Remind.Error");
			Integer days = Integer.valueOf(Integer.parseInt(properties
					.getValue("OR.Remind.Error_own_days")));
			if ("true".equals(remind)) {
				Date allowStartDate = DateUtil.dateAdd(new Date(), days
						.intValue());
				if (DateUtil.compareTwoDay(allowStartDate, of_Bean
						.getStartDate()) == -1)
					return StringUtil.message(msgEarlyDays,
							new Object[] { days });
			}
		}
		if (operate.contains("modify"))
			if (!of_Bean.getIsTiaoxiu().booleanValue()) {
				of_Bean.setOrTiaoxiuHours(null);
				of_Bean.setOrTiaoxiuExpire(null);
			} else if (of_Bean.getOrTiaoxiuHours() == null
					|| of_Bean.getOrTiaoxiuHours().equals(Integer.valueOf(0)))
				return msgTiaoxiu;
		return "SUCC";
	}

	public Overtimerequest objInit(String operate, OvertimeFormBean of_Bean,
			String orUpdateId) {
		String msgIdError = "\u8F93\u5165\u52A0\u73ED\u5355\u53C2\u6570\u6709\u8BEF\uFF0C\u8BF7\u5237\u65B0\u540E\u91CD\u8BD5\uFF01";
		String msgTXHasUsed = "\u52A0\u73ED\u8C03\u4F11\u5355\u5DF2\u7ECF\u88AB\u4F7F\u7528\uFF0C\u4E0D\u80FD\u6539\u4E3A\u975E\u8C03\u4F11\uFF01";
		String msgTXNotEnough = "\u52A0\u73ED\u8C03\u4F11\u5355\u5DF2\u7ECF\u4F7F\u7528\u4E86{0}\u5C0F\u65F6\uFF01";
		Overtimerequest or = new Overtimerequest();
		if (!operate.contains("create")) {
			if (!StringUtils.isEmpty(orUpdateId)) {
				IOvertimerequestBo or_BO = (IOvertimerequestBo) getBean("overtimerequestBO");
				or = or_BO.loadOvertimerequest(orUpdateId);
			}
			if (or == null || StringUtils.isEmpty(or.getOrId())) {
				addErrorInfo(msgIdError);
				return null;
			}
		}
		String fetch[] = { Employee.PROP_EMP_DEPT_NO,
				Employee.PROP_EMP_LOCATION_NO };
		IEmployeeBo empBo = (IEmployeeBo) getBean("empBo");
		Employee emp = empBo.loadEmp(of_Bean.getOrEmpNo().getId(), fetch);
		or.setOrEmpNo(emp);
		or.setOrShiftDate(of_Bean.getOrShiftDate());
		or.setOrStartDate(of_Bean.getStartDate());
		or.setOrEndDate(of_Bean.getEndDate());
		or.setOrTotalHours(BigDecimal.valueOf((double) DateUtil.dateDiff(
				of_Bean.getStartDate(), of_Bean.getEndDate(), 12) / 60D));
		if (operate.contains("create") || operate.contains("update")
				|| operate.equals("mgr-modify") || operate.equals("gm-modify")) {
			or.setOrStartDateApply(of_Bean.getStartDate());
			or.setOrEndDateApply(of_Bean.getEndDate());
			or.setOrTotalHoursApply(or.getOrTotalHours());
		}
		if ((operate.equals("hr-approve") || operate.equals("hr-confirm"))
				&& of_Bean.getIsTiaoxiu().booleanValue()) {
			or.setOrTiaoxiuExpire(of_Bean.getOrTiaoxiuExpire());
			or.setOrTiaoxiuHours(of_Bean.getOrTiaoxiuHours());
			or.setOrTiaoxiuHoursRemain(of_Bean.getOrTiaoxiuHours());
		}
		if (operate.equals("hr-approve")
				&& of_Bean.getIsTiaoxiu().booleanValue()) {
			or.setOrTiaoxiuExpire(of_Bean.getOrTiaoxiuExpire());
			or.setOrTiaoxiuHours(of_Bean.getOrTiaoxiuHours());
			or.setOrTiaoxiuHoursRemain(of_Bean.getOrTiaoxiuHours());
		}
		if (operate.equals("hr-confirm")
				&& of_Bean.getIsTiaoxiu().booleanValue()) {
			double oldTiaoxiuHoursUsed = or.getOrTiaoxiuHours().doubleValue()
					- or.getOrTiaoxiuHoursRemain().doubleValue();
			double newTiaoxiuHours = of_Bean.getOrTiaoxiuHours().doubleValue();
			if (newTiaoxiuHours < oldTiaoxiuHoursUsed) {
				addErrorInfo(StringUtil.message(msgTXNotEnough,
						new Object[] { Double.valueOf(oldTiaoxiuHoursUsed) }));
				return null;
			}
			or.setOrTiaoxiuExpire(of_Bean.getOrTiaoxiuExpire());
			or.setOrTiaoxiuHours(new BigDecimal(newTiaoxiuHours));
			or.setOrTiaoxiuHoursRemain(new BigDecimal(newTiaoxiuHours
					- oldTiaoxiuHoursUsed));
		}
		if (operate.equals("hr-confirm")
				&& !of_Bean.getIsTiaoxiu().booleanValue()
				&& or.getOrTiaoxiuHours() != null) {
			if (or.getOrTiaoxiuHoursRemain().doubleValue() < or
					.getOrTiaoxiuHours().doubleValue()) {
				addErrorInfo(msgTXHasUsed);
				return null;
			}
			or.setOrTiaoxiuExpire(null);
			or.setOrTiaoxiuHours(null);
			or.setOrTiaoxiuHoursRemain(null);
		}
		if (operate.contains("create") || operate.contains("update")) {
			if (!of_Bean.getOrOtNo().equals(or.getOrOtNo())) {
				IOvertimetypeBo ot_BO = (IOvertimetypeBo) getBean("overtimetypeBO");
				Overtimetype ortype = ot_BO.getOvertimetype(of_Bean.getOrOtNo()
						.getOtNo());
				or.setOrOtNo(ortype);
			}
			or.setOrReason(of_Bean.getOrReason());
		}
		if (!wfInit(operate, of_Bean.getOrAppComment(), new Object[] { or }))
			return null;
		else
			return or;
	}

	public String afterInit(Object obj[]) {
		String msgNotCreator = "\u60A8\u4E0D\u662F\u7F16\u53F7\u4E3A{0}\u7684\u52A0\u73ED\u5355\u7684\u521B\u5EFA\u8005\uFF01";
		String msgNoReason = "{0}\u5FC5\u987B\u586B\u5199\u52A0\u73ED\u7406\u7531\uFF01";
		String msgSysError = "\u7CFB\u7EDF\u5F02\u5E38\uFF0C\u8BF7\u5237\u65B0\u540E\u91CD\u8BD5\uFF01";
		String msgCommentsC = "\u521B\u5EFA\uFF1A{0}\u5C0F\u65F6\u7684{1}";
		String msgCommentsU = "\u6539\u4E3A\uFF1A{0}\u5C0F\u65F6\u7684{1}";
		Overtimerequest or = (Overtimerequest) obj[0];
		String operate = or.getWorkflow().getOperate();
		if (operate.contains("update")
				&& !getCurrentEmp().getId().equals(or.getOrEmpNo().getId())
				&& !getCurrentEmp().getId().equals(or.getOrCreateBy().getId()))
			return StringUtil.message(msgNotCreator, new Object[] { or
					.getOrNo() });
		if ((operate.contains("create") || operate.contains("update"))
				&& (or.getOrReason() == null || or.getOrReason().trim().equals(
						"")))
			return StringUtil.message(msgNoReason, new Object[] { or
					.getOrOtNo().getOtName() });
		IOvertimerequestBo overtimerequestBo = (IOvertimerequestBo) getBean("overtimerequestBO");
		String info = null;
		try {
			info = overtimerequestBo.orCheckShifts(or);
			if (info != "SUCC")
				return info;
		} catch (Exception e) {
			e.printStackTrace();
			return msgSysError;
		}
		info = overtimerequestBo.checkMonthLimit(or);
		if (info != "SUCC")
			return info;
		info = overtimerequestBo.orCheckTime(or);
		if (info != "SUCC")
			return info;
		if (operate.contains("create"))
			or.getWorkflow()
					.setComment(
							StringUtil.message(msgCommentsC, new Object[] {
									or.getOrTotalHours(),
									or.getOrOtNo().getOtName() }));
		else if (operate.contains("update"))
			or.getWorkflow()
					.setComment(
							StringUtil.message(msgCommentsU, new Object[] {
									or.getOrTotalHours(),
									or.getOrOtNo().getOtName() }));
		return "SUCC";
	}

	public String wfObjOpBatch(String operate, Object formBean,
			String orUpdateId[]) {
		String msgIEError = "\u6D4F\u89C8\u5668\u53C2\u6570\u5F02\u5E38\uFF0C\u8BF7\u5237\u65B0\u540E\u91CD\u8BD5\uFF01";
		String msgIdError = "\u8F93\u5165\u52A0\u73ED\u5355\u53C2\u6570\u6709\u8BEF\uFF0C\u8BF7\u5237\u65B0\u540E\u91CD\u8BD5\uFF01";
		String msgNoComment = "\u62D2\u7EDD\u7533\u8BF7\u65F6\uFF0C\u5FC5\u987B\u586B\u5199\u5907\u6CE8\uFF01";
		String msgNoData = "\u672A\u9009\u4E2D\u4EFB\u4F55\u6709\u6548\u52A0\u73ED\u5355\uFF0C\u8BF7\u5237\u65B0\u540E\u91CD\u8BD5\uFF01";
		String empNoOriginaldataError = "\u8BF7\u5047\u5355{0}\u7684\u5237\u5361\u6570\u636E\u7F3A\u5931\uFF01";
		OvertimeFormBean of_Bean = (OvertimeFormBean) formBean;
		if (of_Bean == null) {
			addErrorInfo(msgIEError);
			return "input";
		}
		if (orUpdateId == null || orUpdateId.length == 0
				|| StringUtils.isEmpty(orUpdateId[0])) {
			addErrorInfo(msgIdError);
			return "input";
		}
		if (operate.contains("reject")
				&& StringUtils.isEmpty(of_Bean.getOrAppComment())) {
			addErrorInfo(msgNoComment);
			return "input";
		}
		String fetch[] = { Overtimerequest.PROP_OR_EMP_NO,
				Overtimerequest.PROP_OR_EMP_DEPT,
				Overtimerequest.PROP_OR_CREATE_BY,
				Overtimerequest.PROP_OR_OT_NO };
		IOvertimerequestBo or_BO = (IOvertimerequestBo) getBean("overtimerequestBO");
		List orList = or_BO.loadOvertimeRequests(orUpdateId, fetch);
		if (orList == null || orList.size() == 0) {
			addErrorInfo(msgNoData);
			return "input";
		}
		int otMatch = ExaminDateUtil.getOtMatch();
		if ((operate.equals("hr-approve") || operate.equals("tiaoxiu"))
				&& otMatch != 0) {
			String errorMsg = "";
			Iterator i$ = orList.iterator();
			do {
				if (!i$.hasNext())
					break;
				Overtimerequest or = (Overtimerequest) i$.next();
				if (or.getOrEmpNo().getEmpShiftType().intValue() != 0) {
					IAttdoriginaldataBO attdoriginaldataBO = (IAttdoriginaldataBO) getBean("attdoriginaldataBO");
					List empList = new ArrayList();
					empList.add(or.getOrEmpNo());
					List originaldataList = attdoriginaldataBO
							.provideOriginalDataList(or.getOrShiftDate(), or
									.getOrShiftDate(), empList);
					if (originaldataList == null
							|| originaldataList.size() <= 1) {
						errorMsg = StringUtil.addConcatStr(errorMsg, "\u3001",
								new Object[] { or.getOrNo().toString() });
					} else {
						String info = reCalcOrDate(or, otMatch,
								originaldataList);
						if (!"SUCC".equals(info))
							errorMsg = StringUtil.addConcatStr(errorMsg,
									"\u3001", new Object[] { or.getOrNo()
											.toString() });
					}
				}
			} while (true);
			if (!errorMsg.equals("")) {
				addErrorInfo(StringUtil.message(empNoOriginaldataError,
						new Object[] { errorMsg }));
				return "input";
			}
		}
		if (operate.equals("tiaoxiu")) {
			for (int i = 0; i < orList.size(); i++) {
				Overtimerequest temp = (Overtimerequest) orList.get(i);
				temp.setOrTiaoxiuHours(temp.getOrTotalHours());
				temp.setOrTiaoxiuHoursRemain(temp.getOrTotalHours());
				temp.setOrTiaoxiuExpire(getDefaultTiaoxiuExpire(temp));
			}

			operate = "hr-approve";
		}
		if (!wfInit(operate, of_Bean.getOrAppComment(), (Object[]) orList
				.toArray(new Overtimerequest[orList.size()])))
			return "input";
		else
			return wfObjOperate((Object[]) orList
					.toArray(new Overtimerequest[orList.size()]));
	}

	private String reCalcOrDate(Overtimerequest or, int otMatch,
			List originaldataList) {
		String msgAttdOriError = "\u5458\u5DE5{0}\u7684{1}\u52A0\u73ED\u5355{2}\u5237\u5361\u65F6\u95F4\u4E0D\u8DB3\uFF01";
		String msgOrDateError = "\u5458\u5DE5{0}\u7684{1}\u52A0\u73ED\u5355\u65F6\u95F4\u53C2\u6570\u6709\u8BEF\uFF01";
		IEmpshiftBo empShiftBo = (IEmpshiftBo) getBean("empshiftBO");
		Date otDate = or.getOrShiftDate();
		Attendshift as = empShiftBo.getEmpShiftByDate(otDate, or.getOrEmpNo());
		int isInShift = 0;
		if (as != null) {
			Date shiftDT[] = ExaminDateUtil.getSplitDateByAttendShift(otDate,
					as.getAttsSession());
			if (or.getOrStartDate().before(shiftDT[0]))
				isInShift = 1;
			else if (or.getOrEndDate().after(shiftDT[shiftDT.length - 1]))
				isInShift = 2;
			else
				return StringUtil.message(msgOrDateError, new Object[] {
						or.getOrEmpNo().getEmpName(), or.getOrNo() });
		}
		Date orDate[] = calcAttdOriTime(or, originaldataList, Integer
				.valueOf(isInShift), as);
		if (otMatch == 2)
			orDate = calcAttdOriMin(orDate, or, Integer.valueOf(isInShift));
		if (DateUtil.dateDiff(orDate[1], orDate[0], 12) >= 0) {
			return StringUtil
					.message(msgAttdOriError, new Object[] {
							or.getOrEmpNo().getEmpName(),
							or.getOrNo(),
							DateUtil.formatDateToS(or.getOrStartDate(),
									"yyyy-MM-dd") });
		} else {
			double totalHours = (double) DateUtil.dateDiff(orDate[0],
					orDate[1], 12) / 60D;
			or.setOrStartDate(orDate[0]);
			or.setOrEndDate(orDate[1]);
			or.setOrTotalHours(new BigDecimal(totalHours));
			return "SUCC";
		}
	}

	private Date[] calcAttdOriTime(Overtimerequest or, List originaldataList,
			Integer isInShift, Attendshift as) {
		Date orDate[] = new Date[2];
		if (isInShift.intValue() == 0) {
			orDate[0] = DateUtil.formatDate(
					((Attdoriginaldata) originaldataList.get(0))
							.getAodCardTime(), Integer.valueOf(0));
			orDate[1] = DateUtil.formatDate(
					((Attdoriginaldata) originaldataList.get(originaldataList
							.size() - 1)).getAodCardTime(), Integer.valueOf(1));
			return orDate;
		}
		Date shiftDT[] = ExaminDateUtil.getSplitDateByAttendShift(or
				.getOrShiftDate(), as.getAttsSession());
		if (isInShift.intValue() == 1) {
			orDate[0] = DateUtil.formatDate(
					((Attdoriginaldata) originaldataList.get(0))
							.getAodCardTime(), Integer.valueOf(0));
			orDate[1] = shiftDT[0];
			if (or.getOrEndDate().before(orDate[1]))
				orDate[1] = or.getOrEndDate();
		}
		if (isInShift.intValue() == 2) {
			orDate[0] = shiftDT[shiftDT.length - 1];
			if (or.getOrStartDate().after(orDate[0]))
				orDate[0] = or.getOrStartDate();
			orDate[1] = DateUtil.formatDate(
					((Attdoriginaldata) originaldataList.get(originaldataList
							.size() - 1)).getAodCardTime(), Integer.valueOf(1));
		}
		return orDate;
	}

	private Date[] calcAttdOriMin(Date applyDate[], Overtimerequest or,
			Integer isInShift) {
		if ((isInShift.intValue() == 0 || isInShift.intValue() == 1)
				&& DateUtil.dateDiff(applyDate[0], or.getOrStartDate(), 12) > 0)
			applyDate[0] = or.getOrStartDate();
		if ((isInShift.intValue() == 0 || isInShift.intValue() == 2)
				&& DateUtil.dateDiff(or.getOrEndDate(), applyDate[1], 12) > 0)
			applyDate[1] = or.getOrEndDate();
		return applyDate;
	}

	public String wfObjOpSingle(String operate, Object formBean,
			String orUpdateId) {
		String msgIEError = "\u6D4F\u89C8\u5668\u53C2\u6570\u5F02\u5E38\uFF0C\u8BF7\u5237\u65B0\u540E\u91CD\u8BD5\uFF01";
		OvertimeFormBean of_Bean = (OvertimeFormBean) formBean;
		if (of_Bean == null) {
			addErrorInfo(msgIEError);
			return "input";
		}
		if (of_Bean.getOrEmpNo().equals(getCurrentEmp())) {
			if ("hr-create".equals(operate) || "mgr-create".equals(operate))
				operate = "create";
			if ("hr-update".equals(operate) || "mgr-update".equals(operate))
				operate = "update";
		}
		String info = beforeInit(operate, of_Bean);
		if (!info.equals("SUCC")) {
			addErrorInfo(info);
			return "input";
		}
		Overtimerequest or = objInit(operate, of_Bean, orUpdateId);
		if (or == null)
			return "input";
		info = afterInit(new Object[] { or });
		if (!info.equals("SUCC")) {
			addErrorInfo(info);
			return "input";
		} else {
			return wfObjOperate(new Object[] { or });
		}
	}

	public Overtimerequest initOfBean(String operate, OvertimeFormBean of_Bean,
			String orId) {
		String empNoOriginaldataError;
		Overtimerequest or;
		IOvertimerequestBo or_BO;
		label0: {
			String msgIdError = "\u8F93\u5165\u52A0\u73ED\u5355\u53C2\u6570\u6709\u8BEF\uFF0C\u8BF7\u5237\u65B0\u540E\u91CD\u8BD5\uFF01";
			empNoOriginaldataError = "\u5458\u5DE5{0}\u7684{1}\u52A0\u73ED\u5355{2}\u7F3A\u5C11\u5237\u5361\u6570\u636E\uFF01";
			or = new Overtimerequest();
			or_BO = (IOvertimerequestBo) getBean("overtimerequestBO");
			if (!operate.contains("create")) {
				if (orId != null)
					or = or_BO.loadOvertimerequest(orId, new String[] {
							"orEmpDept", "orOtNo", "orEmpNo" });
				if (orId == null || or == null || or.getOrId() == null) {
					addErrorInfo(msgIdError);
					return null;
				}
			}
			IEmployeeBo empBo = (IEmployeeBo) getBean("empBo");
			if (!operate.contains("create"))
				of_Bean.setOrEmpNo(or.getOrEmpNo());
			else if (of_Bean.getOrEmpNo() != null)
				of_Bean.setOrEmpNo(empBo.loadEmp(of_Bean.getOrEmpNo().getId(),
						null));
			else
				of_Bean.setOrEmpNo(empBo.loadEmp(getCurrentEmpNo(), null));
			if (!operate.contains("create") && !operate.contains("update")
					|| of_Bean.getAllOtType() != null)
				break label0;
			IOvertimetypeBo ot_BO = (IOvertimetypeBo) getBean("overtimetypeBO");
			of_Bean.setAllOtType(ot_BO.FindAllOtType());
			Iterator i$ = of_Bean.getAllOtType().iterator();
			Overtimetype ot;
			do {
				if (!i$.hasNext())
					break label0;
				ot = (Overtimetype) i$.next();
			} while (or.getOrOtNo() == null
					|| !or.getOrOtNo().getOtNo().equals(ot.getOtNo()));
			of_Bean.setOrOtNo(ot);
		}
		if (of_Bean.getOrReason() == null)
			of_Bean.setOrReason(or.getOrReason());
		if (of_Bean.getOrShiftDate() == null) {
			Date orShiftDate = or.getOrShiftDate();
			Date orStartDate = or.getOrStartDate();
			Date orEndDate = or.getOrEndDate();
			if (operate.contains("create")) {
				orShiftDate = DateUtil.convDateTimeToDate(new Date());
				IEmpshiftBo empShiftBo = (IEmpshiftBo) SpringBeanFactory
						.getBean("empshiftBO");
				Attendshift as = empShiftBo.getEmpShiftByDate(orShiftDate,
						of_Bean.getOrEmpNo());
				if (as == null) {
					IAttendshiftBO attendBo = (IAttendshiftBO) getBean("attendshiftBO");
					as = attendBo.getDefaultAttendshiftByEmp(of_Bean
							.getOrEmpNo());
				}
				Date dates[] = ExaminDateUtil.getSplitDateByAttendShift(
						orShiftDate, as.getAttsSession());
				orStartDate = dates[dates.length - 1];
				orEndDate = DateUtil.dateTimeAdd(orStartDate, 1, 10);
			}
			if (operate.equals("hr-modify")
					&& or.getOrEmpNo().getEmpShiftType().intValue() != 0) {
				int otMatch = ExaminDateUtil.getOtMatch();
				of_Bean.setOtMatch(Integer.valueOf(otMatch));
				IAttdoriginaldataBO attdoriginaldataBO = (IAttdoriginaldataBO) getBean("attdoriginaldataBO");
				List empList = new ArrayList();
				empList.add(or.getOrEmpNo());
				List originaldataList = attdoriginaldataBO
						.provideOriginalDataList(or.getOrShiftDate(), or
								.getOrShiftDate(), empList);
				if (originaldataList == null || originaldataList.size() <= 1) {
					of_Bean.setErrorMsg(StringUtil.message(
							empNoOriginaldataError, new Object[] {
									or.getOrEmpNo().getEmpName(),
									or.getOrNo(),
									DateUtil.formatDateToS(or.getOrStartDate(),
											"yyyy-MM-dd") }));
				} else {
					of_Bean.setStartDate0(or.getOrStartDate());
					of_Bean.setEndDate0(or.getOrEndDate());
					of_Bean.setTotalHour0(or.getOrTotalHours());
					boolean flag = false;
					String info = reCalcOrDate(or, 1, originaldataList);
					if ("SUCC".equals(info)) {
						flag = true;
						of_Bean.setStartDate1(or.getOrStartDate());
						of_Bean.setEndDate1(or.getOrEndDate());
						of_Bean.setTotalHour1(or.getOrTotalHours());
						or.setOrStartDate(of_Bean.getStartDate0());
						or.setOrEndDate(of_Bean.getEndDate0());
						or.setOrTotalHours(of_Bean.getTotalHour0());
					} else {
						of_Bean.setErrorMsg(info);
					}
					info = reCalcOrDate(or, 2, originaldataList);
					if ("SUCC".equals(info)) {
						of_Bean.setStartDate2(or.getOrStartDate());
						of_Bean.setEndDate2(or.getOrEndDate());
						of_Bean.setTotalHour2(or.getOrTotalHours());
					} else {
						of_Bean.setErrorMsg(info);
					}
					if (otMatch == 0) {
						or.setOrStartDate(of_Bean.getStartDate0());
						or.setOrEndDate(of_Bean.getEndDate0());
						or.setOrTotalHours(of_Bean.getTotalHour0());
					}
					if (otMatch == 1 && flag) {
						or.setOrStartDate(of_Bean.getStartDate1());
						or.setOrEndDate(of_Bean.getEndDate1());
						or.setOrTotalHours(of_Bean.getTotalHour1());
					}
					if (otMatch == 2 && flag) {
						or.setOrStartDate(of_Bean.getStartDate2());
						or.setOrEndDate(of_Bean.getEndDate2());
						or.setOrTotalHours(of_Bean.getTotalHour2());
					}
					orStartDate = or.getOrStartDate();
					orEndDate = or.getOrEndDate();
				}
			}
			of_Bean.setOrShiftDate(orShiftDate);
			if (DateUtil.convDateTimeToDate(orEndDate).after(orShiftDate)) {
				of_Bean.setOrOvernight(Integer.valueOf(1));
				of_Bean.setEndOvernight(Integer.valueOf(1));
			}
			if (DateUtil.convDateTimeToDate(orStartDate).after(orShiftDate))
				of_Bean.setStartOvernight(Integer.valueOf(1));
			of_Bean
					.setStartTime(Integer
							.valueOf(DateUtil.getHour(orStartDate)));
			of_Bean.setStartTimeMinute(Integer.valueOf(DateUtil
					.getMinute(orStartDate)));
			of_Bean.setEndTime(Integer.valueOf(DateUtil.getHour(orEndDate)));
			of_Bean.setEndTimeMinute(Integer.valueOf(DateUtil
					.getMinute(orEndDate)));
		}
		if (operate.contains("modify")) {
			of_Bean.setUsedTime(new BigDecimal(or_BO.getMonthTotal(or,
					Status.PROCESSED)));
			String statusDesc = ComonBeans.getParameterValue("orStatus",
					new String[] { or.getOrStatus().toString() });
			of_Bean.setOtStatus(statusDesc);
			of_Bean.setIsTiaoxiu(Boolean.valueOf(false));
			of_Bean.setOrTiaoxiuHours(or.getOrTiaoxiuHours());
			of_Bean.setOrTiaoxiuExpire(or.getOrTiaoxiuExpire());
			String autoTiaoxiu = (String) DatabaseSysConfigManager
					.getInstance().getProperties().get("sys.examin.ot.tiaoxiu");
			if (operate.equals("hr-modify") && "1".equals(autoTiaoxiu)
					&& or.getOrStatus().intValue() < 15) {
				of_Bean.setIsTiaoxiu(Boolean.valueOf(true));
				of_Bean.setOrTiaoxiuExpire(getDefaultTiaoxiuExpire(or));
			}
			if (of_Bean.getOrTiaoxiuHours() != null) {
				of_Bean.setIsTiaoxiu(Boolean.valueOf(true));
				if (of_Bean.getOrTiaoxiuExpire() == null)
					of_Bean.setOrTiaoxiuExpire(getDefaultTiaoxiuExpire(or));
			}
			ISysLogBO logBO = (ISysLogBO) SpringBeanFactory.getBean("logBO");
			of_Bean.setLogList(logBO.getLogs("overtimerequest", or.getOrId()));
		}
		return or;
	}

	public Date getDefaultTiaoxiuExpire(Overtimerequest or) {
		ILeavetypeBO lt_BO = (ILeavetypeBO) getBean("leavetypeBO");
		Leavetype lt = lt_BO.getLeavetypeByCat(Integer.valueOf(11));
		if (lt != null && StringUtils.isNotEmpty(lt.getLtOtherParameter())) {
			String ltOtherDays = lt.getLtOtherParameter();
			int days = 0;
			if (StringUtil.isNumber(ltOtherDays)) {
				days = Integer.parseInt(ltOtherDays);
				return DateUtil.dateAdd(or.getOrShiftDate(), days);
			} else {
				return getDays(or.getOrShiftDate(), ltOtherDays);
			}
		} else {
			return null;
		}
	}

	private Date getDays(Date date, String days) {
		int length = days.length();
		Integer num = Integer.valueOf(Integer.parseInt(days.substring(0,
				length - 1)));
		String unit = days.substring(length - 1, length);
		if ("d".equals(unit))
			return DateUtil.dateAdd(date, num.intValue());
		if ("m".equals(unit)) {
			return DateUtil.dateTimeAdd(date, num.intValue(), 2);
		} else {
			Date monthStart = DateUtil.getYearMonthFirstDay(date);
			Date otherDate = DateUtil
					.dateTimeAdd(monthStart, num.intValue(), 2);
			return DateUtil.dateAdd(otherDate, -1);
		}
	}

	public void validateDate(ExaminSearchBean es_Bean) {
		if (es_Bean != null) {
			if (es_Bean.getEndDate() != null
					&& es_Bean.getStartDate() != null
					&& es_Bean.getStartDate().getTime() > es_Bean.getEndDate()
							.getTime())
				addErrorInfo("\u5F00\u59CB\u65F6\u95F4\u5E94\u5C0F\u4E8E\u7ED3\u675F\u65F6\u95F4\uFF01");
			if (es_Bean.getEndDate() == null && es_Bean.getStartDate() != null
					|| es_Bean.getEndDate() != null
					&& es_Bean.getStartDate() == null)
				addErrorInfo("\u5FC5\u987B\u540C\u65F6\u586B\u5199\u5F00\u59CB\u548C\u7ED3\u675F\u65E5\u671F\uFF01");
		}
	}

	public void validateOTStatus(ExaminSearchBean es_Bean) {
		if (es_Bean != null && es_Bean.getStatu() != null
				&& es_Bean.getStatu().intValue() != 0
				&& es_Bean.getStatu().intValue() != 22
				&& es_Bean.getStatu().intValue() != 5
				&& es_Bean.getStatu().intValue() != 14
				&& es_Bean.getStatu().intValue() != 15
				&& es_Bean.getStatu().intValue() != 21
				&& es_Bean.getStatu().intValue() != 2
				&& es_Bean.getStatu().intValue() != 16)
			addErrorInfo("Invalidate Status");
	}
}
